OK so I am making a math question game in pygame. I have 8 separate selections so I am spliting them up into functions. Now I have all of my questions and answers in txt files that I am reading into arrays. What I am trying to do is for the first question to pop up, I select my answer, the computer tells if I'm right or wrong, then a button pops up to go to the next question. For some reason, it does not want to loop throught right and the random function alls generates numbers. What am I doing wrong?
import pygame,time,sys,pygbutton,random,time
from pygame.locals import *
pygame.init()
mainclock = pygame.time.Clock()
width = 1024
height = 768
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('Dividing Fractions')
background = pygame.image.load("tv2.png").convert()
background_poistion = [0,0]
screen.blit(background,background_poistion)
pygame.display.flip()
######tester
def tester_Function():
i = 0
questionUNanswered = 1
Section1Q = [0]*5
Section1Ques = open('questions1.txt','r')
with open('questions1.txt','r') as file:
Section1Q = file.readlines()
Section1A = [0]*5
Section1Ans = open('answer1.txt','r')
with open('answer1.txt','r') as file:
Section1A = file.readlines()
num1 = str(random.randint(0,5))
num2 = str(random.randint(0,5))
question = pygbutton.PygButton((100,40,200,30),Section1Q[i])
tester2 = pygbutton.PygButton((100,70,200,30),'Clicl Here')
Ranswer = pygbutton.PygButton((100,110,200,30),Section1A[i])
Wanswer1 = pygbutton.PygButton((100,150,200,30),num1)
Wanswer2 = pygbutton.PygButton((100,190,200,30),num2)
winner = pygbutton.PygButton((500,190,200,30),'')
question.draw(screen)
tester2.draw(screen)
Ranswer.draw(screen)
Wanswer1.draw(screen)
Wanswer2.draw(screen)
winner.draw(screen)
buttonEvent = Ranswer.handleEvent(event)
buttonEvent2= Wanswer1.handleEvent(event)
buttonEvent3= Wanswer2.handleEvent(event)
if 'click' in buttonEvent:
winner = pygbutton.PygButton((500,190,200,30),'Winner')
winner.draw(screen)
i = i+1
if 'click' in buttonEvent2 :
winner = pygbutton.PygButton((500,190,200,30),'Lose')
winner.draw(screen)
i = i+1
if 'click' in buttonEvent3 :
winner = pygbutton.PygButton((500,190,200,30),'Lose')
winner.draw(screen)
i= i +1
question.draw(screen)
tester2.draw(screen)
Ranswer.draw(screen)
Wanswer1.draw(screen)
Wanswer2.draw(screen)
winner.draw(screen)
while True: # main game loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
tester_Function()
pygame.display.update()
In main loop you call tester_Function() so every loop you starts at the beginning. Every loop you read file, set i to zero, create buttons, etc. Some functions should be done before main loop.
Related
I started using pygame and I want to do simple game. One of the elements which I need is countdown timer.
How can I do the countdown time (eg 10 seconds) in PyGame?
Another easy way is to simply use pygame's event system.
Here's a simple example:
import pygame
pygame.init()
screen = pygame.display.set_mode((128, 128))
clock = pygame.time.Clock()
counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(3) if counter > 0 else 'boom!'
if e.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
pygame.display.flip()
clock.tick(60)
On this page you will find what you are looking for http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
You download ticks once before beginning the countdown (which can be a trigger in the game - the key event, whatever).
For example:
start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
if seconds>10: # if more than 10 seconds close the game
break
print (seconds) #print how many seconds
In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:
timer_interval = 500 # 0.5 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event , timer_interval)
Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.
To disable the timer for an event, set the milliseconds argument to 0.
Receive the event in the event loop:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == timer_event:
# [...]
The timer event can be stopped by passing 0 to the time parameter.
See the example:
import pygame
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 10
text = font.render(str(counter), True, (0, 128, 0))
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 1000)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
counter -= 1
text = font.render(str(counter), True, (0, 128, 0))
if counter == 0:
pygame.time.set_timer(timer_event, 0)
window.fill((255, 255, 255))
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
pygame.display.flip()
pygame.time.Clock.tick returns the time in milliseconds since the last clock.tick call (delta time, dt), so you can use it to increase or decrease a timer variable.
import pygame as pg
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
font = pg.font.Font(None, 40)
gray = pg.Color('gray19')
blue = pg.Color('dodgerblue')
# The clock is used to limit the frame rate
# and returns the time since last tick.
clock = pg.time.Clock()
timer = 10 # Decrease this to count down.
dt = 0 # Delta time (time since last tick).
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
timer -= dt
if timer <= 0:
timer = 10 # Reset it to 10 or do something else.
screen.fill(gray)
txt = font.render(str(round(timer, 2)), True, blue)
screen.blit(txt, (70, 70))
pg.display.flip()
dt = clock.tick(30) / 1000 # / 1000 to convert to seconds.
if __name__ == '__main__':
main()
pg.quit()
There are several ways you can do this- here's one. Python doesn't have a mechanism for interrupts as far as I know.
import time, datetime
timer_stop = datetime.datetime.utcnow() +datetime.timedelta(seconds=10)
while True:
if datetime.datetime.utcnow() > timer_stop:
print "timer complete"
break
There are many ways to do this and it is one of them
import pygame,time, sys
from pygame.locals import*
pygame.init()
screen_size = (400,400)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("timer")
time_left = 90 #duration of the timer in seconds
crashed = False
font = pygame.font.SysFont("Somic Sans MS", 30)
color = (255, 255, 255)
while not crashed:
for event in pygame.event.get():
if event.type == QUIT:
crashed = True
total_mins = time_left//60 # minutes left
total_sec = time_left-(60*(total_mins)) #seconds left
time_left -= 1
if time_left > -1:
text = font.render(("Time left: "+str(total_mins)+":"+str(total_sec)), True, color)
screen.blit(text, (200, 200))
pygame.display.flip()
screen.fill((20,20,20))
time.sleep(1)#making the time interval of the loop 1sec
else:
text = font.render("Time Over!!", True, color)
screen.blit(text, (200, 200))
pygame.display.flip()
screen.fill((20,20,20))
pygame.quit()
sys.exit()
This is actually quite simple. Thank Pygame for creating a simple library!
import pygame
x=0
while x < 10:
x+=1
pygame.time.delay(1000)
That's all there is to it! Have fun with pygame!
Another way to do it is to set up a new USEREVENT for a tick, set the time interval for it, then put the event into your game loop
'''
import pygame
from pygame.locals import *
import sys
pygame.init()
#just making a window to be easy to kill the program here
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption("tick tock")
#set tick timer
tick = pygame.USEREVENT
pygame.time.set_timer(tick,1000)
while 1:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.USEREVENT:
if event.type == tick:
## do whatever you want when the tick happens
print('My tick happened')
I'm new to pygame, and I'm trying to practice skill with 'Plants and Zombies', but here comes a problem when I run the code: the next bullet does not move to the edge of the screen, it ends at a point at which the previous bullet collide with a zombie. I can NOT find out where is the problem, hope someone can help me out. Just run main.py, click on a top menu(weapon), right click on any point of a grass, u'll know the problem.
project directory:
helper.py:
import os
from PIL import Image
import pygame
current_dir = os.getcwd()
def get_path(image_name):
return os.path.join(current_dir, "resource", image_name)
def get_image_size(image_name):
with Image.open(get_path(image_name)) as im:
return im.size
def load_image_source(image_name, with_bullet=False):
if with_bullet:
bullet_name = "bullet_"+image_name.replace("shooter_", "")
return pygame.image.load(get_path(image_name)), pygame.image.load(get_path(bullet_name))
else:
return pygame.image.load(get_path(image_name))
def load_all_shooters():
weapon = {"selected": [], "unselected": []}
shooter_dir = os.path.join(current_dir, "resource")
for shooter in os.listdir(shooter_dir):
if "shooter" in shooter:
if "selected" in shooter:
weapon["selected"].append(shooter)
else:
weapon["unselected"].append(shooter)
weapon["selected"] = sorted(weapon["selected"])
weapon["unselected"] = sorted(weapon["unselected"])
return weapon
def get_nearest_position(source_list, target_number):
return min(source_list, key=lambda x: abs(x - target_number))
config.py:
LINES = 5
ZOMBIE_SPEED_X = 1
ZOMBIE_REFRESH_TIME = 3000
BULLET_REFRESH_TIME = 1000
BULLET_SPEED = 3
SHOOTER_SIZE = (50, 50)
main.py:
import pygame
import sys
import random
from ZombiesVSPlants.common.helper import get_nearest_position, load_image_source, load_all_shooters, get_image_size, get_path, get_path
from ZombiesVSPlants.config.config import BULLET_SPEED, BULLET_REFRESH_TIME, ZOMBIE_REFRESH_TIME, SHOOTER_SIZE, ZOMBIE_SPEED_X, LINES
pygame.init()
# top weapon menu
menu_height = 60
# get background grass and other resource path and size...
background_grass_path = get_path("background_grass.png")
zombie_path = get_path("enemy_zombie.png", )
single_line_height = get_image_size("background_grass.png")[1]
zombie_size = get_image_size("enemy_zombie.png")
# screen size
screen_height = LINES*single_line_height+menu_height
screen_width = 800
# background colour, zombie speed
background_color = 255, 255, 255
ZOMBIE_SPEED = [-ZOMBIE_SPEED_X, 0]
# others
LINES_LIST = [i for i in range(1, LINES+1)]
zombie_start_x = screen_width-zombie_size[0]
zombie_start_y = (single_line_height-zombie_size[1])/2
shooter_centered_position__list_y = [line*single_line_height+zombie_start_y+menu_height for line in range(5)]
# resource boom
boom = load_image_source("boom.png")
# dragging and other global variables
dragging = False
mouse_follow = None
mouse_follow_rect = None
added_shooters = []
shooter_bullets = []
bullets_rect = []
collide_zombies = []
collide_bullets = []
# screen size
screen = pygame.display.set_mode((screen_width, screen_height))
# load top weapon surface
res = load_all_shooters()
menu_start_position = 5, 5
menu_shooters = [load_image_source(unselected_shooter, with_bullet=True) for unselected_shooter in res["unselected"]]
menu_shooters_selected = [load_image_source(selected_shooter) for selected_shooter in res["selected"]]
menu_shooters_rect = [pygame.Rect(menu_start_position[0]+55*i, menu_start_position[1], SHOOTER_SIZE[0], SHOOTER_SIZE[1]) for i in range(len(menu_shooters))]
# background grass surface
grass = load_image_source(background_grass_path)
grass_rect = [pygame.Rect(0, i * single_line_height+menu_height, screen_width, single_line_height) for i in range(LINES)]
# the very first zombie surface
zombie = load_image_source(zombie_path)
zombies_rect = [pygame.Rect(zombie_start_x, zombie_start_y+menu_height, zombie_size[0], zombie_size[1])]
# ZOMBIE_REFRESH_TIME
NEW_ZOMBIE_EVENT = pygame.USEREVENT+1
pygame.time.set_timer(NEW_ZOMBIE_EVENT, ZOMBIE_REFRESH_TIME)
# BULLET_REFRESH_TIME
NEW_BULLET_EVENT = pygame.USEREVENT+2
pygame.time.set_timer(NEW_BULLET_EVENT, BULLET_REFRESH_TIME)
while 1:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# left click on a top menu(weapon), dragging is enabled
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
for i in range(len(menu_shooters)):
if menu_shooters_rect[i].collidepoint(event.pos):
dragging = not dragging
mouse_follow = menu_shooters[i]
# right click when dragging, new weapon will be set on a point
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
x = event.pos[0]
if event.pos[1] > menu_height and dragging:
dragging = not dragging
y = get_nearest_position(shooter_centered_position__list_y, event.pos[1])
added_shooters.append([mouse_follow[0], x, y])
shooter_bullets.append([x, y, mouse_follow[1]])
# spawn new bullet
if event.type == NEW_BULLET_EVENT:
for j in range(len(shooter_bullets)):
bullets_rect.append([shooter_bullets[j][2], pygame.Rect(shooter_bullets[j][0], shooter_bullets[j][1], 15, 15)])
# spawn new zombie
if event.type == NEW_ZOMBIE_EVENT:
# random in roads that new zombies will appear
new_zombies_count = random.randint(1, LINES)
new_zombies_lines = random.sample(LINES_LIST, new_zombies_count)
# add to zombies list
for line in new_zombies_lines:
new_zombie_rect = pygame.Rect(zombie_start_x, line * single_line_height + zombie_start_y + menu_height, zombie_size[0], zombie_size[1])
zombies_rect.append(new_zombie_rect)
# blit top weapons menu
for i in range(len(menu_shooters)):
menu_rect = menu_shooters_rect[i]
screen.blit(menu_shooters[i][0], menu_rect)
# blit selected weapon if mouse hover on a weapon menu
for i in range(len(menu_shooters)):
if menu_shooters_rect[i].collidepoint(pygame.mouse.get_pos()):
screen.blit(menu_shooters_selected[i], menu_shooters_rect[i])
# blit background grass
for r in grass_rect:
screen.blit(grass, r)
# blit all the weapons on the grass
for new in added_shooters:
shooter_rect = pygame.Rect(new[1], new[2], 50, 50)
screen.blit(new[0], shooter_rect)
# blit bullets
for j in range(len(bullets_rect)):
bullets_rect[j][1].x += 1
screen.blit(bullets_rect[j][0], bullets_rect[j][1])
# blit zombies
for i in range(len(zombies_rect)):
zombies_rect[i].x -= ZOMBIE_SPEED_X
screen.blit(zombie, zombies_rect[i])
# blit weapon follows mouse move position
if dragging and mouse_follow[0]:
pos_follow_mouse = pygame.mouse.get_pos()
mouse_follow_rect = pygame.Rect(pos_follow_mouse[0], pos_follow_mouse[1], 50, 50)
screen.blit(mouse_follow[0], mouse_follow_rect)
# collide between zombie and bullet
for i in range(len(bullets_rect)):
for j in range(len(zombies_rect)):
if bullets_rect[i][1].colliderect(zombies_rect[j]):
print("collide!")
screen.blit(boom, zombies_rect[j])
collide_bullets.append(bullets_rect[i])
collide_zombies.append(zombies_rect[j])
bullets_rect = [i for i in bullets_rect if i not in collide_bullets]
zombies_rect = [j for j in zombies_rect if j not in collide_zombies]
pygame.display.flip()
I now got a compromise like solution, when need to remove surface in a collide between 2 rects in 2 lists, just draw the 2 lists in 2 separate for loop and remove the collided rect in another for loop, the demo code would be:
# draw bullets
for b in bullets_rect:
b_r = b[1]
b_r.x += BULLET_SPEED
screen.blit(b[0], b_r)
# draw zombies
for z in zombies_rect:
z.x += -ZOMBIE_SPEED_X
screen.blit(zombie, z)
# when collide, use list.remove to remove collided rect
for i, j in itertools.product(bullets_rect, zombies_rect):
b_r = i[1]
z_r = j
if z_r.contains(b_r):
# when collide, remove the collided rect
screen.blit(boom, z_r)
bullets_rect.remove(i)
zombies_rect.remove(j)
FYI.
Just started learning Python/Pygame watching videos and reading to learn . I would like to see a example code to cycle 3 images on a rect button from a mouse press and return to first image. Really I want the pictures to be three options and return different results. So be able to cycle image be able to select option and option when triggered execute choice.
Example
import pygame
pygame.init()
screen = pygame.display.set_mode((300,200))
# three images
images = [
pygame.Surface((100,100)),
pygame.Surface((100,100)),
pygame.Surface((100,100)),
]
images[0].fill((255,0,0))
images[1].fill((0,255,0))
images[2].fill((0,0,255))
# image size and position
images_rect = images[0].get_rect()
# starting index
index = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# check mouse position and pressed button
if event.button == 1 and images_rect.collidepoint(event.pos):
# cycle index
index = (index+1) % 3
screen.blit(images[index], images_rect)
pygame.display.flip()
pygame.quit()
Example using class - to create many buttons
import pygame
class Button(object):
def __init__(self, position, size):
self._images = [
pygame.Surface(size),
pygame.Surface(size),
pygame.Surface(size),
]
self._images[0].fill((255,0,0))
self._images[1].fill((0,255,0))
self._images[2].fill((0,0,255))
self._rect = pygame.Rect(position, size)
self._index = 0
def draw(self, screen):
screen.blit(self._images[self._index], self._rect)
def event_handler(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and self._rect.collidepoint(event.pos):
self._index = (self._index+1) % 3
pygame.init()
screen = pygame.display.set_mode((320,110))
button1 = Button((5, 5), (100, 100))
button2 = Button((110, 5), (100, 100))
button3 = Button((215, 5), (100, 100))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
button1.event_handler(event)
button2.event_handler(event)
button3.event_handler(event)
button1.draw(screen)
button2.draw(screen)
button3.draw(screen)
pygame.display.flip()
pygame.quit()
If I understood the question correctly, you need a single button that changes the look every time you click on it, and changes its relative function accordingly.
You should be able to solve your problem by creating a class that takes two list and a counter
1) list of images
2) list of functions
3) the counter tells you which image/function is selected.
The functions needs to be built in the class, but you can provide the image that you want in the class argument (actually, you could pass them as arguments, but I don't think is worth it).
Here is the code, I commented some lines with their intended meaning (in line comments)
import pygame
import sys
pygame.init()
width = 600
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Magic Buttons")
background = pygame.Surface(screen.get_size())
clock = pygame.time.Clock()
class Buttons:
def __init__(self, posX, posY, image1, image2, image3):
self.image_list = [image1, image2, image3] # static list of images
self.function_list = [self.button_function_1,self.button_function_2,self.button_function_3 ]
self.rect_position = (posX, posY) # this is a tuple to identify the upper left corner of the rectangle of the image
self.button_type = 0 # initial value of the button, both for the function and the image
self.image = pygame.image.load(self.image_list[0]) #default image, index number 0 of image_list
self.rect = pygame.Rect(posX, posY, self.image.get_width(), self.image.get_height()) # create a rectangle object same size of the images
def check(self, pos):
if self.rect.collidepoint(pos) ==True:
self.change_button()
else:
pass
def change_button(self):
self.button_type = (self.button_type +1)%3
self.image = pygame.image.load(self.image_list[self.button_type ]) # load the image relative to button_type
self.function_list[self.button_type -1]() # execute the function relative to the new value of button_type
self.draw_button()
def draw_button(self):
screen.blit(self.image, self.rect_position) # blit the new button image
def button_function_1(self):
print ("function 1 in action")
def button_function_2(self):
print ("function 2 in action")
def button_function_3(self):
print ("function 3 in action")
multibutton = Buttons(100,100,"button1.png","button2.png","button3.png") # create an istance of the button in the x=100, y = 100, with the three image button
while True:
background.fill((0,0,0))
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos() # fetch the position of the mouse
multibutton.check(pos) # check if the mouse is on the button
multibutton.draw_button()
pygame.display.flip()
i am totally new to python. thus please explain thoroughly wen u respond to my query.
in my code i want to measure the time the player lasts before his cursor touches the bouncing ball on the screen but the time i am obtaining is the sum of all the seconds since the first time the command was initialised. i need to stop the counting of seconds as soon as the user ends the game and to begin afresh wen the play again option is chosen...
also i wud lyk a way to make the player qualify for the next level without usin d detection of position of mouse....since dat is making the program code logic incorrect...
here is my code:
import pygame,sys
from pygame.locals import*
from pygame import *
import random
def main():
# set up sounds
# import pygame
#gameOverSound = pygame.mixer.Sound('gameover.wav')
#pygame.mixer.music.load('background.mid')
#from pygame import *
#import random
#pygame.mixer.pre_init(44100, 16, 2, 4096)
#background =image.load('C:\Users\Administrator\Desktop\oearth.png')
ballpic = image.load('ball1.png')
ballpic.set_colorkey((0,0,0))
#find a way to include verification of more than one colour key
# to add more than one shape....pending
numballs = 10
delay = 5
done = False
balls = []
stars = []
#generate an if loop for execution of both the parts
k = 2
init()
screen = display.set_mode((640, 480))
display.set_caption('mouse game by shivangi and ananya')
event.set_grab(1)
#pygame.time.Clock() Creates a Clock object (assign this to a name), which you can then call the tick() method on
#to find out how much time has passed since the last time you called tick()
#pygame.time.delay(milliseconds) Pauses game for time specified
#pygame.time.get_ticks() Returns the number of milliseconds passed since pygame.init() was #called
for count in range(numballs):
balls.append(dict)
balls[count] = {'x': 0, 'y': 0, 'xmove': random.randint(1, 2), 'ymove': random.randint(1, 2)}
screen = display.set_mode((640, 480)) #####screen = display.set_mode((640,480),FULLSCREEN,32).....check functioning...
#pygame.display.list_modes()
#[(800, 600), (1280, 1024), (1280, 960), (1280, 800), (1280, 768), (1280, 720),
#(1152, 864), (1088, 612), (1024, 768), (960, 600), (848, 480), (800, 600),
#(720, 576), (720, 480), (640, 480), (640, 400), (512, 384), (480, 360), (400, 300),
#(320, 240), (320, 200), (640, 480)]
while done == False:
pygame.time.Clock()
init()
screen.fill((0,100,0))
for count in range(numballs):
screen.blit(ballpic, (balls[count]['x'], balls[count]['y']))
display.update()
time.delay(delay)
for count in range(numballs):
balls[count]['x'] = balls[count]['x'] + balls[count]['xmove']
balls[count]['y'] = balls[count]['y'] + balls[count]['ymove']
for count in range(numballs):
if balls[count]['x'] > 620:
balls[count]['xmove'] = random.randint(-2, 0)
if balls[count]['x'] < -10:
balls[count]['xmove'] = random.randint(0, 6)
if balls[count]['y'] > 470:
balls[count]['ymove'] = random.randint(-9, 0)
if balls[count]['y'] < -10:
balls[count]['ymove'] = random.randint(0, 5)
for e in event.get():
if e.type == KEYUP:
if e.key == K_ESCAPE:
done = True
if screen.get_at((mouse.get_pos())) == (227,209,43):
done = True
if done == True:
#pygame.mixer.init()
break
sec = time.get_ticks()/1000
quit()
while sec < k:
print "you lasted for only",sec,"seconds...try again to qualify for the next level..."
quit()
break
time_lasted = 0
while sec >= k:
starpic = image.load('star.png')
starpic.set_colorkey((0,0,0))
#find a way to include verification of more than one colour key...PENDING
numstars = 30
delay = 8
done = False
stars = []
for count in range(numstars):
stars.append(dict)
stars[count] = {'x': 0, 'y': 0, 'xmove': random.randint(1, 2), 'ymove': random.randint(1, 2)}
screen = display.set_mode((640, 480))
display.set_caption('mouse game')
event.set_grab(1)
while done == False:
init()
screen.fill((160,32,240))
for count in range(numstars):
screen.blit(starpic, (stars[count]['x'], stars[count]['y']))
display.update()
time.delay(delay)
for count in range(numstars):
stars[count]['x'] = stars[count]['x'] + stars[count]['xmove']
stars[count]['y'] = stars[count]['y'] + stars[count]['ymove']
for count in range(numstars):
if stars[count]['x'] > 620:
stars[count]['xmove'] = random.randint(-2, 0)
if stars[count]['x'] < -10:
stars[count]['xmove'] = random.randint(0, 2)
if stars[count]['y'] > 470:
stars[count]['ymove'] = random.randint(-2, 0)
if stars[count]['y'] < -10:
stars[count]['ymove'] = random.randint(0, 2)
for e in event.get():
if e.type == KEYUP:
if e.key == K_ESCAPE:
done = True
if screen.get_at((mouse.get_pos())) == (255,255,255,255):
done = True
if done == True:
time_lasted = time.get_ticks()/1000
quit()
break
#sec = time.update()
#time_lasted = time.update()
#correction of time error to be done....pending....
quit()
print "You lasted for", sec, "seconds in the first level!"
print "\n"
print "you lasted for",time_lasted,"in the second level!"
print "\n"
print "your total score is :", int(sec) + int(time_lasted)
print "\n"
print "game over...! Thank you for playing!! :D :P :)"
quit()
break
main()
x = 1
x = raw_input("Do you want to play again?")
while x.upper() in ['YES','Y']:
main()
x = raw_input("Do you want to play again?")
while x.upper() in ['NO','N']:
break
# when player starts game
start = time.get_ticks()
# during game
current = time.get_ticks()
time_you_played = (current - start)/1000
EDIT:
Timer to count seconds.
class Timer():
def __init__(self):
self._start = 0
def start(self):
self._start = pygame.time.get_ticks()
def current(self):
return (pygame.time.get_ticks() - self._start)/1000
Example - how to use:
import pygame
# add class Timer here
pygame.init()
t = Timer()
t.start() # start or restart timer
pygame.time.wait(3000) # for test only
print t.current() # print seconds
pygame.time.wait(2000) # for test only
print t.current() # print seconds
You don't have to stop timer because you can use start() to restart timer.
Ok so for our couse,we have pretty much been thrown alot of code and told to make a project of it...been fixing bugs all week.Now i finally got a scrolling background working,but when I add it.The shoot functionality stops working,as well as the controls...I need help as to why this is happening? The project is due in a week and I'm running out of options..
""" Import al the necessary classes/sprites and pygame libs """
import pygame
from sys import exit
from LaserSprite import Laser
from PlayerShipSprite import PlayerShip
from EnenySprite import Asteroid
from Vector_Calc import Vector
from CursorSprite import Cursor
from ScoreBoardSprite import ScoreBoard
import math
""" Set up the screen size and sound mixer """
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.mixer.init()
""" This function updates the score board with
information retrieved from the player class """
def updateScoreBoard(RefreshScore):
(ship, scoreBoard) = RefreshScore
scoreBoard.text = ("Score: %d Fired: %d Shield: %d" %(ship.Get_Score(),ship.Get_Ammo(),ship.Get_Shield()))
""" This function updates the position of the ship on the
screen based on what keys are pressed """
def checkKeys(myData):
(event, ship) = myData
if event.key == pygame.K_LEFT:
print 'LEFT'
ship.MoveLeft()
if event.key == pygame.K_RIGHT:
print 'RIGHT'
ship.MoveRight()
if event.key == pygame.K_UP:
ship.MoveUp()
print 'UP'
if event.key == pygame.K_DOWN:
print 'DOWN'
ship.MoveDown()
""" This is the main game loop, when the game is over the player will
be returned to the introduction screen """
def game():
""" Set up the title of the game, the background size, color and then
blit to the screen """
pygame.display.set_caption("Asteroids Version 2.0")
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
screen.blit(background, (0, 0))
""" Set the mouse cursor to be hidden """
pygame.mouse.set_visible(False)
""" Create a new instance of a player and cursor, here
I have set up the cursor as a class the will be updated during the game """
ship = PlayerShip(screen)
cursor = Cursor()
""" Create a new instance of the ScoreBoard class, this is then
given a text value and a position on the screen. Pay note
as to how the score, ammo and shield are retrieved from the PlayerShip class"""
scoreBoard = ScoreBoard()
scoreBoard.text = ("Score: %d Fired: %d Shield: %d" %(ship.Get_Score(),ship.Get_Ammo(),ship.Get_Shield()))
scoreBoard.center = (320,470)
""" Create empty sprite groups as shown below """
all_sprites_list = pygame.sprite.Group()
laser_list = pygame.sprite.Group()
asteroid_list = pygame.sprite.Group()
""" Create 20 asteroids and add them to the asteroid list group """
for i in range(10):
asteroid = Asteroid()
asteroid_list.add(asteroid)
""" Add the ship, cursor, asteroid list group and the scoreBoard to the
all sprites list. In doing this we can have the transparent effect some
were looking for """
all_sprites_list.add(ship)
all_sprites_list.add(cursor)
all_sprites_list.add(asteroid_list)
all_sprites_list.add(scoreBoard)
""" Set up the refresh rate and key repeat for the game"""
clock = pygame.time.Clock()
pygame.key.set_repeat(10,10)
keepGoing = True
while keepGoing:
b1 = "ocean1.gif"
back = pygame.image.load(b1)
back2 = pygame.image.load(b1)
h = 0
screenWidth = 600
clock.tick(30)
""" Check for any events - keyboard, mouse etc. """
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
screen.blit(back, (h,0))
screen.blit(back2, (h-screenWidth,0))
h = h + 0.5
if h == screenWidth:
h = 0
if event.type == pygame.KEYDOWN:
""" If a key is pressed, bundle the event and the ship
and pass it to the function that handles events """
myData = (event, ship)
checkKeys(myData)
if event.type == pygame.MOUSEBUTTONDOWN:
""" If a mouse event occurs check to see if it is the left click """
if event.button==1:
""" reduce the ammo of the ship
get the center of the ship sprite
get where the mouse was clicked on the screen
bundle the (x,y) position for the above
pass them to PlotVector function and have the
angle and vector returned
create a new instance a laser
pass to it the start position, angle and vector it must travel
add the laser to the laser_list group and the all sprites group """
ship.Set_Ammo();
shipCenter = ship.returnPosition()
mousePos = pygame.mouse.get_pos()
print "Ship: %s, Mouse: %s " %(shipCenter, mousePos)
data= (shipCenter, mousePos)
angle, vect = PlotVector(data)
laser = Laser()
laser.fire.play()
laser.AnglePoints(shipCenter, angle, vect)
laser_list.add(laser)
all_sprites_list.add(laser)
""" update all sprites """
all_sprites_list.update()
""" For every laser that was fired we are going to do the following: """
for laser in laser_list:
""" Create a list of asteroids that were hit by the laser """
asteroid_hit_list = pygame.sprite.spritecollide(laser, asteroid_list, False)
""" For each asteroid hit,
Update the ship score
play a bang sound
reset the asteroid to the top of the screen again
remove the laser from the laser list and all sprites """
for asteroid in asteroid_hit_list:
ship.Set_Score()
asteroid.bang.play()
asteroid.reset()
laser_list.remove(laser)
all_sprites_list.remove(laser)
""" Remove the laser if it flies up off the screen """
if laser.rect.y < -10 or laser.rect.y > screen.get_height():
laser_list.remove(laser)
all_sprites_list.remove(laser)
if laser.rect.x < -10 or laser.rect.x > screen.get_width():
laser_list.remove(laser)
all_sprites_list.remove(laser)
""" Now we are going to create a list of asteroids that hit the ship """
asteroid_hit_Ship = pygame.sprite.spritecollide(ship, asteroid_list, False)
""" For each asteroid that hits the ship,
Update the ship shields
play a bang sound
reset the asteroid to the top of the screen again """
for asteroid in asteroid_hit_Ship:
asteroid.bang.play()
ship.Set_Shield()
asteroid.reset()
""" if the ship's shields are less than 0 then the game will finish """
if ship.Get_Shield()<0:
keepGoung=False
break
""" Update the score and refresh the scoreboard using the updateScoreBoard function """
RefreshScore = (ship, scoreBoard)
updateScoreBoard(RefreshScore)
""" The last part refreshes the sprites """
asteroid_list.clear(screen, background)
asteroid_list.update()
asteroid_list.draw(screen)
all_sprites_list.clear(screen, background)
all_sprites_list.update()
all_sprites_list.draw(screen)
pygame.display.flip()
return ship.Get_Score()
def PlotVector(PlotVect):
"""
This PlotVector function handles the calculation of the new
vector and also the calculation of the angle the arrow/bullet/projectile
will be transformed and travel to.
REFER TO YOUR NOTES FROM LECTURE 5
"""
""" Un-bundle the data that is passed into this function """
(start, dest) = PlotVect
""" Create a new Vector object and call it vect """
vect = Vector()
""" Pass the start and dest coordinates and get back a new vector which the arrow must travel """
vect = Vector.from_points(start, dest)
""" Calculate the magnitude (Distance) between the two points """
mag = vect.get_magnitude()
""" Get the values for the vector, i.e. the change in x and change in y """
x = vect.x
y = vect.y
""" This variable will be used to calculate and store the angle between points """
angDEG = (math.atan2(y, x)*(180/math.pi))*-1
""" Print the coordinates and angle to the screen for testing """
print "Start: %s \nEnd: %s\nVector: %s\nMagnitude: %d" %(start, dest, vect, mag)
print "Angle : %d" %(angDEG)
""" Bundle and return the angle and vector which will be used in the TheArrow Sprite """
return (angDEG, vect)
"""
The next if statement runs the main function as it is in the primary program scope.
This was covered in lecture 5 available on Moodle
"""
def instructions(score):
shipIntro = PlayerShip(screen)
allSprites = pygame.sprite.Group(shipIntro)
insFont = pygame.font.SysFont(None, 30)
instructions = (
"Asteroids Version 2.0" ,
"Previous Score: %d" %score,
"Instructions: You must clear the asteroid",
"field so that you can reach your home-world",
"",
"Fly around the field and fire to hit the ",
"asteroids but be careful not to fly too close",
"to any asteroids. Your ship has a limited amount of ",
"off shielding and if your shielding drops below ",
"0 your done for",
"good luck!",
"",
"click to start, escape to quit..."
)
insLabels = []
for line in instructions:
tempLabel = insFont.render(line, 1, (255, 255, 0))
insLabels.append(tempLabel)
keepGoing = True
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while keepGoing:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
donePlaying = True
if event.type == pygame.MOUSEBUTTONDOWN:
keepGoing = False
donePlaying = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
keepGoing = False
donePlaying = True
allSprites.update()
allSprites.draw(screen)
for i in range(len(insLabels)):
screen.blit(insLabels[i], (50, 30*i))
pygame.display.flip()
pygame.mouse.set_visible(True)
return donePlaying
def main():
donePlaying = False
score = 0
while not donePlaying:
donePlaying = instructions(score)
if not donePlaying:
score = game()
if __name__ == "__main__":
main()
Check indentions - you have if event.type == pygame.KEYDOWN: and if event.type == pygame.MOUSEBUTTONDOWN: outside of for event in pygame.event.get():. You have some screen.blit() betwin for and if with smaller indentions.
Maybe you put this in wrong place:
screen.blit(back, (h,0))
screen.blit(back2, (h-screenWidth,0))
h = h + 0.5
if h == screenWidth:
h = 0
EDIT:
Probably your code should look like this:
while keepGoing:
b1 = "ocean1.gif"
back = pygame.image.load(b1)
back2 = pygame.image.load(b1)
h = 0
screenWidth = 600
clock.tick(30)
""" Check for any events - keyboard, mouse etc. """
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
""" If a key is pressed, bundle the event and the ship
and pass it to the function that handles events """
myData = (event, ship)
checkKeys(myData)
elif event.type == pygame.MOUSEBUTTONDOWN:
""" If a mouse event occurs check to see if it is the left click """
if event.button == 1:
""" reduce the ammo of the ship
get the center of the ship sprite
get where the mouse was clicked on the screen
bundle the (x,y) position for the above
pass them to PlotVector function and have the
angle and vector returned
create a new instance a laser
pass to it the start position, angle and vector it must travel
add the laser to the laser_list group and the all sprites group """
ship.Set_Ammo();
shipCenter = ship.returnPosition()
mousePos = pygame.mouse.get_pos()
print "Ship: %s, Mouse: %s " %(shipCenter, mousePos)
data= (shipCenter, mousePos)
angle, vect = PlotVector(data)
laser = Laser()
laser.fire.play()
laser.AnglePoints(shipCenter, angle, vect)
laser_list.add(laser)
all_sprites_list.add(laser)
screen.blit(back, (h,0))
screen.blit(back2, (h-screenWidth,0))
h = h + 0.5
if h == screenWidth:
h = 0
# rest of main loop
btw: I use elif because event.type can't be pygame.QUIT, pygame.KEYDOWN and pygame.MOUSEBUTTONDOWN in one event.