how do i update time in my program? - pygame

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.

Related

How can i move the Instances of the game on shifted y axis? [duplicate]

I'm a beginner programmer who is starting with python and I'm starting out by making a game in pygame.
The game basically spawns circles at random positions and when clicked, it gives you points.
Recently I've hit a roadblock when I want to spawn multiple instances of the same object (in this case circles) at the same time.
I've tried stuff like sleep() and some other code related to counters, but it always results in the next circle spawned overriding the previous one (i.e the program spawns circle 1, but when circle 2 comes in, circle 1 disappears).
Does anyone know a solution to this? I would really appreciate your help!
import pygame
import random
import time
pygame.init()
window = pygame.display.set_mode((800,600))
class circle():
def __init__(self, color, x, y, radius, width,):
self.color = color
self.x = x
self.y = y
self.radius = radius
self.width = width
def draw(self, win, outline=None):
pygame.draw.circle(win, self.color, (self.x, self.y, self.radius, self.width), 0)
run=True
while run:
window.fill((0, 0, 0))
pygame.draw.circle(window, (255, 255, 255), (random.randint(0, 800),random.randint(0, 600)), 20, 20)
time.sleep(1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
pygame.quit()
quit()
It does not work that way. time.sleep, pygame.time.wait() or pygame.time.delay is not the right way to control time and gameplay within an application loop. The game does not respond while you wait. The application loop runs continuously. You have to measure the time in the loop and spawn the objects according to the elapsed time.
pygame.Surface.fill clears the entire screen. Add the newly created objects to a list. Redraw all of the objects and the entire scene in each frame.
See also Time, timer event and clock
You have 2 options. Use pygame.time.get_ticks() to measure the time. Define a time interval after which a new object should appear. Create an object when the point in time is reached and calculate the point in time for the next object:
object_list = []
time_interval = 500 # 500 milliseconds == 0.1 seconds
next_object_time = 0
while run:
# [...]
current_time = pygame.time.get_ticks()
if current_time > next_object_time:
next_object_time += time_interval
object_list.append(Object())
Minimal example:
repl.it/#Rabbid76/PyGame-TimerSpawnObjects
import pygame, random
pygame.init()
window = pygame.display.set_mode((300, 300))
class Object:
def __init__(self):
self.radius = 50
self.x = random.randrange(self.radius, window.get_width()-self.radius)
self.y = random.randrange(self.radius, window.get_height()-self.radius)
self.color = pygame.Color(0)
self.color.hsla = (random.randrange(0, 360), 100, 50, 100)
object_list = []
time_interval = 200 # 200 milliseconds == 0.2 seconds
next_object_time = 0
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
current_time = pygame.time.get_ticks()
if current_time > next_object_time:
next_object_time += time_interval
object_list.append(Object())
window.fill(0)
for object in object_list[:]:
pygame.draw.circle(window, object.color, (object.x, object.y), round(object.radius))
object.radius -= 0.2
if object.radius < 1:
object_list.remove(object)
pygame.display.flip()
pygame.quit()
exit()
The other option is to use the pygame.event module. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds. e.g.:
object_list = []
time_interval = 500 # 500 milliseconds == 0.1 seconds
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_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.
Receive the event in the event loop:
while run:
for event in pygame.event.get():
if event.type == timer_event:
object_list.append(Object())
The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.
Minimal example:
repl.it/#Rabbid76/PyGame-TimerEventSpawn
import pygame, random
pygame.init()
window = pygame.display.set_mode((300, 300))
class Object:
def __init__(self):
self.radius = 50
self.x = random.randrange(self.radius, window.get_width()-self.radius)
self.y = random.randrange(self.radius, window.get_height()-self.radius)
self.color = pygame.Color(0)
self.color.hsla = (random.randrange(0, 360), 100, 50, 100)
object_list = []
time_interval = 200 # 200 milliseconds == 0.2 seconds
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_interval)
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
object_list.append(Object())
window.fill(0)
for object in object_list[:]:
pygame.draw.circle(window, object.color, (object.x, object.y), round(object.radius))
object.radius -= 0.2
if object.radius < 1:
object_list.remove(object)
pygame.display.flip()
pygame.quit()
exit()

Making a countdown timer in a Quiz game with pygame [duplicate]

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')

How to delay an event in Pygame without delaying all other functions?

I have this game where a balloon needs to explode every 3 seconds, but the explosion needs to be delayed for about 1 second (right now, it's almost immediate). I have the balloon every 3 seconds part down, but I'm having trouble on the delay part. If I use sleep or wait, all the other animations I have going on stop, and that's not what I want. Does anyone have any tips?
Here's a complete example. I highly recommend using object-oriented programming, pygame sprites and sprite groups. These sprites have a timer attribute which is decreased by the delta time, dt, each frame. When the time of a balloon is up, it is removed from the containing sprite groups balloon.kill() and an explosion instance is added instead. The explosion also has a timer and removes itself, self.kill(), when the timer is below 0. (Press a mouse button to add more balloons.)
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Images. Balloon = blue, explosion = orange.
BALLOON_IMAGE = pg.Surface((50, 50), pg.SRCALPHA)
pg.draw.circle(BALLOON_IMAGE, pg.Color('steelblue2'), (25, 25), 25)
EXPLOSION_IMAGE = pg.Surface((80, 80), pg.SRCALPHA)
pg.draw.circle(EXPLOSION_IMAGE, pg.Color('sienna1'), (40, 40), 40)
class Balloon(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = BALLOON_IMAGE
self.rect = self.image.get_rect(center=pos)
self.timer = 3
def update(self, dt):
self.timer -= dt
class Explosion(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = EXPLOSION_IMAGE
self.rect = self.image.get_rect(center=pos)
self.timer = 1
def update(self, dt):
self.timer -= dt
if self.timer <= 0:
self.kill()
balloons = pg.sprite.Group(Balloon((300, 300)))
all_sprites = pg.sprite.Group(balloons)
done = False
while not done:
dt = clock.tick(30) / 1000
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
balloon = Balloon(event.pos)
balloons.add(balloon)
all_sprites.add(balloon)
all_sprites.update(dt)
for balloon in balloons:
if balloon.timer <= 0:
balloon.kill()
all_sprites.add(Explosion(balloon.rect.center))
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pg.display.flip()
pygame.time.get_ticks() gives current time in milliseconds.
You can set (3s = 3000ms)
next_baloon = pygame.time.get_ticks() + 3*1000
and in every loop you have to check if it is time to start ballon
if next_baloon <= pygame.time.get_ticks():
start_ballon()
next_baloon = pygame.time.get_ticks() + 3*1000
next_explosion = pygame.time.get_ticks() + 1*1000
if next_explosion <= pygame.time.get_ticks():
explode_ballon()

Quiz based game in pygame

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.

pygame - losing functionality when scrolling background is added

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.