Lustiges Ballfangspiel
- python lustiges ballspiel1_2_0.py
# -*- coding: utf-8 -*- cheat = raw_input("Dein Name:") import pygame import random dx = 5.7 # x speed vector of the ball in pixel per frame dy = 5.9 # y speed vector of the ball in pixel per frame pygame.init() screen=pygame.display.set_mode((640,480)) screenrect = screen.get_rect() background = pygame.Surface(screen.get_size()) background.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255))) # fill the background white (red,green,blue) background = background.convert() # faster blitting ballsurface = pygame.Surface((50,50)) # create a new surface (black by default) ballsurface.set_colorkey((0,0,0)) # make black the transparent color (red,green,blue) #pygame.draw.circle(Surface, color, pos, radius, width=0) pygame.draw.circle(ballsurface, (0,0,255), (25,25),25) # paint blue circle ballsurface = ballsurface.convert_alpha() # faster blitting, convert_alpha() because transparency screen.blit(background, (0,0)) #draw background on screen (overwriting all) ballrect = ballsurface.get_rect() # the rectangle of the ball surface, for collision detection ballx = 300 # left ball position bally = 240 screen.blit(ballsurface, (ballx, bally)) #draw the ball surface (lines will draw over this ball) ballx2 = 400 # right ball position bally2 = 380 clock = pygame.time.Clock() mainloop = True FPS = 30 # desired framerate in frames per second. try out other values ! playtime = 0.0 t = 0 # variable used to draw a pattern color1 = 0 color2 = 0 farbdauer = 0 punkte=0 while mainloop: if cheat == "igottime": zeit = 99999999 else: zeit = 10-playtime if zeit <= 0: break pygame.draw.circle(ballsurface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (25,25),25) pygame.draw.circle(background, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (250,25),25) milliseconds = clock.tick(FPS) # do not go faster than this framerate seconds = milliseconds / 1000.0 playtime += milliseconds / 1000.0 farbdauer += seconds #if farbdauer >2: if abs(pygame.mouse.get_pos()[0]-ballx2) < 25: if abs(pygame.mouse.get_pos()[1]-bally2)< 25: balx=random.randint(0,600) baly=random.randint(0,400) ballx = random.randint(0,600) # left ball position bally = random.randint(0,400) ballx2 = random.randint(0,600) # right ball position bally2 = random.randint(0,400) farbdauer = 0 background.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255))) pygame.draw.circle(background, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (balx,baly),25) pygame.draw.circle(ballsurface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (25,25),25) background.blit(ballsurface, (ballx, bally)) screen.blit(background, (0,0)) punkte = punkte+1 for event in pygame.event.get(): if event.type == pygame.QUIT: mainloop = False # pygame window closed by user elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: mainloop = False # user pressed ESC # ------- draw cute pattern ------------------ pygame.draw.line(screen, (color1,255-color1,color2), (32*t,0), (0,480-24*t)) pygame.draw.line(screen, (255-color2,color2,color1), (32*t,480), (640,480-24*t)) screen.blit(ballsurface, (ballx2, bally2)) #draw the ball over the lines t += 1 # increase t if t > 20: t = 0 # reset t color1 = random.randint(0,255) # new color color2 = random.randint(0,255) # --------- end of cute pattern drawing code ---------- #bouncing ball#### try: dirtyrect = background.subsurface((ballx2-5,bally2-5,ballrect.width+10, ballrect.height+10)) # calculate clean rect screen.blit(dirtyrect, (ballx2-5,bally2-5)) # blit clean rect on top of "dirty" screen except: screen.blit(background,(0,0)) ballx2 += dx bally2 += dy # bounce ball if out of screen if ballx2 < 0: ballx2 = 0 dx *= -1 #elif ballx + ball.get_width() > screen.get_width(): elif ballx2 + ballrect.width > screenrect.width: ballx2 = screenrect.width - ballrect.width dx *= -1 if bally2 < 0: bally2 = 0 dy *= -1 elif bally2 + ballrect.height > screenrect.height: bally2= screenrect.height - ballrect.height dy *= -1 # paint the ball screen.blit(ballsurface, (round(ballx2,0), round(bally2,0))) #end bouncing pygame.display.set_caption("Punkte = %i ZEIT: %.2f sekunden" % (punkte,zeit)) pygame.display.flip() # flip the screen 30 times a second print "This 'game' was played for %.2f seconds." % playtime print "Du hast %i Punkte erreicht!!!! online highscore = %i"%(punkte,random.randint(punkte,50))
Version 1.2.0:
bug fixes: dirty rect
New: bouncing ball



