Hey Guys!
i started learning python a month ago (2 to 3 times a week) now Im creating a simple game and i Really need your help!
So, the game goes like this_
"New (additional) Red squares" will be added(spawned and remain) on screen each time the "White square" eats a "Green square"
as simple as that :)
Please keep in mind: that my code is sloppy and not efficient and i should probably use classes. :)
Here is the code:
import pygame, sys
from random import randint
from tkinter import*
from pygame.locals import*
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
xxx = randint(10, 780)
yyy = randint(10, 580)
block_width = 15
block_height = 15
block_color = pygame.Color("green")
block = pygame.Rect(xxx, yyy, block_width, block_height)
xx = randint(20, 780)
yy = randint(20, 580)
obstacle_width = 10
obstacle_height = 10
obstacle_color = pygame.Color("red")
obstacle = pygame.Rect(xx, yy, obstacle_width, obstacle_height)
a = 400
b = 300
hero_WIDTH = 10
hero_HEIGHT = 10
heroColor = pygame.Color("white")
hero = pygame.Rect(a, b, hero_WIDTH, hero_HEIGHT)
'''Heath Bar'''
def x():
x = a - (health_WIDTH/2) + (hero_WIDTH/2)
return x
def y():
y = b - (health_HEIGHT/2) - (hero_HEIGHT/3)
return y
health_HEIGHT = 3
health_WIDTH = 32
healthColor = pygame.Color("green")
health = pygame.Rect(x(), y(), health_WIDTH, health_HEIGHT)
score =0
while True:
clock.tick(60)
'''When removing (line below), the square creates a line behind it!!!'''
gameDisplay.fill((0, 20, 5))
gameDisplay.fill(heroColor, hero)
gameDisplay.fill(block_color, block )
gameDisplay.fill(healthColor, health)
gameDisplay.fill(obstacle_color, obstacle)
'''Score Text'''
white = (255, 255, 255)
message = "score: " + str(int(score))
font = pygame.font.Font(None, 40)
text = font.render(message, 1, white)
gameDisplay.blit(text, (10, 10))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if hero.colliderect(block):
block = pygame.Rect(randint(10, 780), randint(10, 580), block_width, block_height)
score += int(block_width) * int(block_height) / 10
elif hero.colliderect(obstacle):
obstacle = pygame.Rect(randint(5, 790), randint(5, 590), obstacle_width, obstacle_height)
score -= int(obstacle_width) * int(obstacle_height)/10
hero.inflate_ip(1, 1)
health.inflate_ip(1, 0)
'''Hero'''
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
hero.left = hero.left - 10
if keys[K_RIGHT]:
hero.right = hero.right + 10
if keys[K_UP]:
hero.top = hero.top - 10
if keys[K_DOWN]:
hero.bottom = hero.bottom + 10
if keys[K_SPACE]:
hero.space = hero.space
'''Health'''
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
health.left = health.left - 10
if keys[K_RIGHT]:
health.right = health.right + 10
if keys[K_UP]:
health.top = health.top - 10
if keys[K_DOWN]:
health.bottom = health.bottom + 10
if keys[K_SPACE]:
health.space = health.space
Aucun commentaire:
Enregistrer un commentaire