Markus Hauser
Byte of Python lesen: http://www.swaroopch.com/notes/Python
Pygame gibt's hier: http://pygame.org/
Ezmenu: ezmenu.zip
schweifdemo
- schweifdemo.py
# -*- coding: utf-8 -*- """wizard duell by markus, mira, raphael and theresa. """ import pygame import random import os print pygame.ver def megaultrasuperfatdef(humanplayers=1,datadir="data"): HITPOINTS = 100.0 FORCE_OF_GRAVITY = 10 # in pixel per second² .See http://en.wikipedia.org/wiki/Gravitational_acceleration def write(msg="pygame is cool"): """write text into pygame surfaces""" myfont = pygame.font.SysFont("None", 100) mytext = myfont.render(msg, True, (0,0,0)) mytext = mytext.convert_alpha() return mytext def neudelta(): return random.randint(1, 20) def farbcheck(farbe, delta): if farbe > 255: farbe=255 delta*=-1 if farbe < 0: farbe=0 delta = neudelta() return farbe,delta class dummysound: def play(self): pass def load_sound(file): if not pygame.mixer: return dummysound() file = os.path.join(datadir, file) try: sound = pygame.mixer.Sound(file) return sound except pygame.error: print ("Warning, unable to load,", file) return dummysound() pygame.mixer.pre_init(44100,-16,2,2048) pygame.init() PEACEZEIT = .5 if pygame.mixer and not pygame.mixer.get_init(): print ('Warning, no sound') pygame.mixer = None #load the sound effects bump = load_sound('bump.ogg') tock = load_sound('tock.ogg') winna = load_sound('winner.ogg') if pygame.mixer: music = os.path.join(datadir,'an-turr.ogg') pygame.mixer.music.load(music) pygame.mixer.music.play(-1) class Hirni(pygame.sprite.Sprite): hirnis = {} number = 0 def __init__(self,spieler,pos,links=pygame.K_LEFT,rechts=pygame.K_RIGHT, unten=pygame.K_DOWN,oben=pygame.K_UP): pygame.sprite.Sprite.__init__(self, self.groups) #print "Hirni startet" self.pos=pos self.links=links self.rechts=rechts self.unten=unten self.oben=oben self.spieler=spieler self.rot = random.randint(10,200) self.gruen = random.randint(10,200) self.blau = random.randint(10,200) self.peacezeit = 0 self.deltarot = random.randint(1,20) self.deltagruen = random.randint(1,20) self.deltablau = random.randint(1,20) self.image=pygame.Surface((50,50)) self.farbwechsel() self.hitpointsfull = HITPOINTS # maximal hitpoints self.hitpoints = HITPOINTS # actual hitpoints self.image.set_colorkey((0,0,0)) self.image=self.image.convert_alpha() self.rect=self.image.get_rect() self.rect.center=pos self.dx = 0.0 self.dy = 0.0 self.x = self.pos[0] + 0.0 self.y = self.pos[1] + 0.0 self.number = Hirni.number # get my personal Birdnumber Hirni.number+= 1 # increase the number for next Bird Hirni.hirnis[self.number] = self #Livebar(self) #create a Livebar for him. self.catched = False self.crashing = False def kill(self): """because i want to do some special effects (sound, directory etc.) before killing the Bird sprite i have to write my own kill(self) function and finally call pygame.sprite.Sprite.kill(self) to do the 'real' killing""" tock.play() winna.play() #print Bird.birds, "..." for _ in range(random.randint(100,200)): Fragment((self.x,self.y),2) Hirni.hirnis[self.number] = None # kill Bird in sprite Directory pygame.sprite.Sprite.kill(self) def farbwechsel(self): self.rot += self.deltarot self.gruen += self.deltagruen self.blau += self.deltablau self.rot, self.deltarot = farbcheck(self.rot, self.deltarot) self.gruen, self.deltagruen = farbcheck(self.gruen, self.deltagruen) self.blau, self.deltablau = farbcheck(self.blau, self.deltablau) #pygame.draw.circle(self.image,(self.rot,self.gruen,self.blau), # (25,25),25) pygame.draw.circle(self.image, (self.rot,self.gruen,self.blau), (25,25),10) self.image=self.image.convert_alpha() def cleanstatus(self): self.catched = False self.crashing = False def update(self,seconds): #print "hirni updated sich und startet einen schweif" Schweif(self.pos,(self.rot,self.gruen,self.blau), self.deltarot, self.deltagruen, self.deltablau) self.farbwechsel() tasten = pygame.key.get_pressed() if self.peacezeit > 0: self.peacezeit-=seconds else: self.peacezeit=0 if self.spieler == "human": #sorry for bad code if tasten[self.links]: self.dx -= 1 if tasten[self.rechts]: self.dx += 1 if tasten[self.oben]: self.dy -= 1 if tasten[self.unten]: self.dy += 1 self.dx *= 0.995 # bremsen self.dy *= 0.995 if self.dx > 500: self.dx = 500 elif self.dx < -500: self.dx = -500 if self.dy > 500: self.dy = 500 elif self.dy < -500: self.dy = -500 self.x += self.dx * seconds self.y += self.dy * seconds if self.x - self.rect.width/2 < 0: self.x = 0 + self.rect.width/2 self.dx *= -1 tock.play() elif self.x + self.rect.width/2 > screen.get_width(): self.x = screen.get_width() - self.rect.width/2 self.dx *= -1 tock.play() if self.y - self.rect.height/2 < 0: self.y = 0 + self.rect.height/2 self.dy *= -1 tock.play() elif self.y + self.rect.height/2 > screen.get_height(): self.y = screen.get_height() - self.rect.height/2 self.dy *= -1 tock.play() self.pos = (self.x, self.y) self.rect.centerx = round(self.x, 0) self.rect.centery = round(self.y, 0) #--- loose hitpoins if self.crashing: self.hitpoints -=1 #--- check if still alive if self.hitpoints <= 0: if self.number==0: # findet raus ob der zauberer oder der player gestorben ist. screen.blit(write("WTF!"),(150,10)) background.blit(write("wtf!"),(150,10)) screen.blit(write("You lose!"),(300,60)) background.blit(write("You lose!"),(300,60)) self.kill() else: # findet raus ob der zauberer oder der player gestorben ist. screen.blit(write("WTF!"),(150,10)) background.blit(write("wtf!"),(150,10)) screen.blit(write("You win!"),(350,60)) background.blit(write("You win!"),(350,60)) self.kill() class Schweif(pygame.sprite.Sprite): """ist völlig unnötig""" def __init__(self,pos,farbe, deltarot, deltagruen, deltablau): pygame.sprite.Sprite.__init__(self,self.groups) self.image = pygame.Surface((25,25)) self.image.set_colorkey((0,0,0)) pygame.draw.circle(self.image,farbe,(12,12),10) self.rect = self.image.get_rect() self.time=0 self.pos=pos self.farbe = farbe self.rot = self.farbe[0] self.gruen = self.farbe[1] self.blau = self.farbe[2] self.deltarot = deltarot self.deltagruen = deltagruen self.deltablau = deltablau self.rect.centerx = round(self.pos[0],0) self.rect.centery = round(self.pos[1],0) self.schweiftime = 3# länge vom Schweif in sekunden def update(self, time): self.time+=time if self.time >= self.schweiftime: self.kill() class Mine(pygame.sprite.Sprite): """liegt rum und macht bumm""" images = [] images.append(pygame.image.load(os.path.join(datadir,"mine1.png"))) images.append(pygame.image.load(os.path.join(datadir,"mine2.png"))) def __init__(self,pos,bombenleger): pygame.sprite.Sprite.__init__(self,self.groups) self.image = Mine.images[0] self.rect = self.image.get_rect() self.bombenleger = bombenleger self.pos = pos self.rect.center = self.pos self.time = 0 #self.lebendig = True def update(self, time): self.time += time self.image = Mine.images[int(self.time % 2)] #if self.time > 5: # self.kill() def kill(self): """because i want to do some special effects (sound, directory etc.) before killing the mine sprite i have to write my own kill(self) function and finally call pygame.sprite.Sprite.kill(self) to do the 'real' killing""" bump.play() #print Bird.birds, "..." for _ in range(random.randint(3,100)): Fragment(self.pos) #Bird.birds[self.number] = None # kill mine in sprite Directory pygame.sprite.Sprite.kill(self) # kill the actual mine class Fragment(pygame.sprite.Sprite): """a fragment of an exploding Bird""" gravity = True # fragments fall down ? def __init__(self, pos, style=1): pygame.sprite.Sprite.__init__(self, self.groups) self.pos = [0.0,0.0] self.pos[0] = pos[0] self.pos[1] = pos[1] self.style=style self.image = pygame.Surface((50,50)) self.image.set_colorkey((0,0,0)) # black transparent if style == 2: pygame.draw.circle(self.image, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (25,25), random.randint(0,45)) else: pygame.draw.circle(self.image, (random.randint(100,150),122,255), (25,25), random.randint(0,15)) self.image = self.image.convert_alpha() self.rect = self.image.get_rect() self.lifetime = 1 + random.random()*5 # max 6 seconds self.time = 0.0 self.fragmentmaxspeed = 523 # try out other factors ! self.dx = random.randint(-self.fragmentmaxspeed,self.fragmentmaxspeed) self.dy = random.randint(-self.fragmentmaxspeed,self.fragmentmaxspeed) def update(self, seconds): self.time += seconds if self.time > self.lifetime: self.kill() self.pos[0] += self.dx * seconds self.pos[1] += self.dy * seconds if Fragment.gravity: self.dy += FORCE_OF_GRAVITY # gravity suck fragments down self.rect.centerx = round(self.pos[0],0) self.rect.centery = round(self.pos[1],0) class Livebar(pygame.sprite.Sprite): """shows a bar with the hitpoints of a ball sprite""" def __init__(self, boss): pygame.sprite.Sprite.__init__(self,self.groups) self.boss = boss self.image = pygame.Surface((self.boss.rect.width,7)) self.image.set_colorkey((0,0,0)) # black transparent pygame.draw.rect(self.image, (0,255,0), (0,0,self.boss.rect.width,7),1) self.rect = self.image.get_rect() self.oldpercent = 0 self.bossnumber = self.boss.number def update(self, time): self.percent = self.boss.hitpoints / self.boss.hitpointsfull * 1.0 if self.percent != self.oldpercent: pygame.draw.rect(self.image, (0,0,0), (1,1,self.boss.rect.width-2,5)) # fill black pygame.draw.rect(self.image, (0,255,0), (1,1, int(self.boss.rect.width * self.percent),5),0) # fill green self.oldpercent = self.percent self.rect.centerx = self.boss.rect.centerx self.rect.centery = self.boss.rect.centery - self.boss.rect.height /2 - 10 #check if boss is still alive if not Hirni.hirnis[self.bossnumber]: self.kill() # kill the hitbar #breite=640 #hoehe=480 screen=pygame.display.set_mode((0,0)) # try out larger values and see what happens ! background = pygame.Surface(screen.get_size()) rot=random.randint(0,255) gruen=random.randint(0,255) blau=random.randint(0,255) background.fill((rot,gruen,blau)) #fill the background white (red,green,blue) #pygame.draw.circle(background, (50,50,0), (300,250), 200) # draw a circle on the background #pygame.draw.circle(background, (255,255,255), (300,250), 150) background = background.convert() screen.blit(background,(0,0)) deltarot = neudelta() deltagruen = neudelta() deltablau = neudelta() clock = pygame.time.Clock() mainloop = True FPS = 60 # desired framerate in frames per second. hirnigruppe=pygame.sprite.Group() minengruppe = pygame.sprite.Group() fragmentgruppe = pygame.sprite.Group() livebargruppe = pygame.sprite.Group() schweifgroup = pygame.sprite.Group() allgroup = pygame.sprite.LayeredUpdates() #hirn1=Hirni((random.randint(0,640),random.randint(0,480))) Hirni.groups = allgroup, hirnigruppe Schweif.groups = allgroup, schweifgroup Mine.groups = minengruppe, allgroup Fragment.groups = fragmentgruppe, allgroup Livebar.groups = livebargruppe, allgroup Hirni._layer = 2 print "start hirn1" hirn1 = Hirni("human",(50,55)) if humanplayers ==2: hirn2 = Hirni("human",(590,430), pygame.K_a,pygame.K_d,pygame.K_s,pygame.K_w) else: hirn2 = Hirni("AI",(590,430)) hirn3 = Hirni("AI",(200,430)) hirn4 = Hirni("AI",(200,430)) gameover = False overtime = 9.3 #hirnigruppe.add(hirn1,hirn2,hirn3,hirn4) while mainloop: # do all this each frame milliseconds = clock.tick(FPS) # milliseconds passed since last frame seconds = milliseconds / 1000.0 # seconds passed since last frame if gameover: overtime -= seconds if overtime < 0: mainloop = False 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 #if event.key == pygame.K_SPACE: #loesch() #background.fill((rot,gruen,blau)) #fill the background white (red,green,blue) #screen.blit(background,(0,0)) #if event.key == pygame.K_LCTRL: #if hirn2.peacezeit==0: #Mine(hirn2.rect.center,2) #hirn2.peacezeit=PEACEZEIT #if event.key == pygame.K_RCTRL: #if hirn1.peacezeit==0: #Mine(hirn1.rect.center,1) #hirn1.peacezeit=PEACEZEIT #print("Habe Mine gelegt, und bin ein cooles Kind!") tasten = pygame.key.get_pressed() if hirn2.peacezeit==0: if hirn2.spieler == "human": if tasten[pygame.K_LCTRL]: Mine(hirn2.rect.center, 2) else: #Mine(hirn2.rect.center,2) pass hirn2.peacezeit=PEACEZEIT if tasten[pygame.K_RCTRL]: if hirn1.peacezeit==0: Mine(hirn1.rect.center,1) hirn1.peacezeit=PEACEZEIT rot+=deltarot gruen+=deltagruen blau+=deltablau if len(hirnigruppe) < 2: gameover = True if hirn2.spieler == "AI": if hirn2.x < hirn1.x: hirn2.dx += 10 if hirn2.x > hirn1.x: hirn2.dx -= 10 if hirn2.y < hirn1.y: hirn2.dy += 10 if hirn2.y > hirn1.y: hirn2.dy -= 10 if hirn3.spieler == "AI": if hirn3.x < hirn2.x: hirn3.dx += 5 if hirn3.x > hirn2.x: hirn3.dx -= 5 if hirn3.y < hirn2.y: hirn3.dy += 5 if hirn3.y > hirn2.y: hirn3.dy -= 5 if hirn4.spieler == "AI": if hirn4.x < hirn3.x: hirn4.dx += 5 if hirn4.x > hirn3.x: hirn4.dx -= 5 if hirn4.y < hirn3.y: hirn4.dy += 5 if hirn4.y > hirn3.y: hirn4.dy -= 5 rot,deltarot=farbcheck(rot,deltarot) gruen,deltagruen=farbcheck(gruen,deltagruen) blau,deltablau=farbcheck(blau,deltablau) #background.fill((rot,gruen,blau)) #fill the background white (red,green,blue) #screen.blit(background,(0,0)) pygame.display.set_caption("fps %.2f dx %.2f" % (clock.get_fps(), hirn1.dx)) # only blit the part of the background where the ball was (cleanrect) #cleanrect = background.subsurface((ballx, bally, ball.get_width(), ball.get_height())) #screen.blit(cleanrect, (ballx, bally)) # comment out this line for a funny effect ! #calculate new center of ball # time based movement. No matter how busy the cpu and how low the framerate, # movement speed will always be constant. #hirnigruppe.clear(screen,background) crashgroup = pygame.sprite.spritecollide(hirn1, minengruppe, False) for crash in crashgroup: if crash.bombenleger == 1: print("Ich bin auf eigene Mine gestiegen, und bin ein cooles Kind!") else: print("Ich bin auf fremde Mine getreten, und bin ein cooles Kind!") hirn1.hitpoints -= 10 crash.kill() megacrashgroup = pygame.sprite.spritecollide(hirn1, hirnigruppe, False) for megacrash in megacrashgroup: if megacrash.number == 0: pass else: megacrash.dx *= -1 megacrash.dy *= -1 crashgroup2 = pygame.sprite.spritecollide(hirn2, minengruppe, False) for crash in crashgroup2: if crash.bombenleger == 2: print("Ich bin auf eigene Mine gestiegen, und bin ein cooles Kind!") else: print("Ich bin auf fremde Mine getreten, und bin ein cooles Kind!") hirn2.hitpoints -= 10 crash.kill() #pygame.sprite.groupcollide(group1, group2, dokill1, dokill2): return Sprite_dict #crashgroup = pygame.sprite.groupcollide(hirnigruppe, minengruppe, False, True) #minengruppe.clear(screen, background) #fragmentgruppe.clear(screen, background) #fragmentgruppe.update(seconds) #livebargruppe.clear(screen,background) #minengruppe.update(seconds) #hirnigruppe.update(seconds) #livebargruppe.update(seconds) #minengruppe.draw(screen) #hirnigruppe.draw(background) #hirnigruppe.draw(screen) #fragmentgruppe.draw(screen) #livebargruppe.draw(screen) # ----------- clear, draw , update, flip ----------------- allgroup.clear(screen, background) #print seconds allgroup.update(seconds) allgroup.draw(screen) #pygame.display.flip() pygame.display.flip() # flip the screen 30 times a second if __name__ == "__main__": megaultrasuperfatdef() pygame.quit()
DER HIRNSCHONER
Download:hirnschoner.zip
Hirnschoner 2:hirni.zip Musik von: http://modarchive.org/index.php?request=view_by_moduleid&query=33502
Nie wieder Aspirin kaufen! Der Hirnschoner hilft bei akuten Kopfschmerzen oder Stress viel besser. Gebrauchsanleitung: Bei plötzlichem Stress, oder Kopfschmerzen alle Arbeit liegen lassen, und Hirnschoner starten! Einfach zugucken und entspannen. Und für alle die mitmachen wollen, kann man den Ball mit W,A,S und D, das Quadrat mit den Pfeiltasten steuern. Selbstverständluch kann man auch alles mit der Maus steuern.
Code
# -*- coding: utf-8 -*- """bouncing ball markus hauser 2009 """ import pygame import random import os def neudelta(): return random.randint(1, 20) def loesch(): screen.blit(background, (0, 0)) def farbcheck(farbe, delta): if farbe > 255: farbe=255 delta*=-1 if farbe < 0: farbe=0 delta = neudelta() return farbe,delta class dummysound: def play(self): pass def load_sound(file): if not pygame.mixer: return dummysound() file = os.path.join('data', file) try: sound = pygame.mixer.Sound(file) return sound except pygame.error: print ("Warning, unable to load,", file) return dummysound() pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print ('Warning, no sound') pygame.mixer = None #load the sound effects bump = load_sound('bump.ogg') tock = load_sound('tock.ogg') #if pygame.mixer: #music = os.path.join('data', 'house_lo.wav') #pygame.mixer.music.load(music) #pygame.mixer.music.play(-1) breite=640 hoehe=480 screen=pygame.display.set_mode((breite,hoehe)) # try out larger values and see what happens ! background = pygame.Surface(screen.get_size()) background.fill((255,255,255)) #fill the background white (red,green,blue) #pygame.draw.circle(background, (50,50,0), (300,250), 200) # draw a circle on the background #pygame.draw.circle(background, (255,255,255), (300,250), 150) background = background.convert() ball = pygame.Surface((50,50)) #create a new surface ball.set_colorkey((0,0,0)) #make black the transparent color (red,green,blue) quadrat = pygame.Surface((50,50)) quadratx=100 quadraty=100 #pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect breite = 1 #linienbreite ballrot=55 ballgruen=200 ballblau=0 quadratrot=0 quadratgruen=30 quadratblau=200 deltarot = neudelta() deltagruen = neudelta() deltablau = neudelta() quadratdeltarot = neudelta() quadratdeltagruen = neudelta() quadratdeltablau = neudelta() pygame.draw.circle(ball, (ballrot,ballgruen,ballblau), (25,25),25) # paint blue circle ball = ball.convert() ballx = 550 bally = 240 dx = 60 # pixel per second ! ball dy = 60 # pixel per second ! ball qx = 0 # quadrat qy = 0 #quadrat screen.blit(background, (0,0)) #draw background on screen (overwriting all) screen.blit(ball, (ballx, bally)) #draw the ball shape clock = pygame.time.Clock() mainloop = True FPS = 60 # desired framerate in frames per second. dist=0 distx=0 disty=0 while mainloop: # do all this each frame milliseconds = clock.tick(FPS) # milliseconds passed since last frame seconds = milliseconds / 1000.0 # seconds passed since last frame clock.tick(FPS) # do not go faster than this framerate 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 if event.key == pygame.K_SPACE: loesch() # mausbewegung if pygame.mouse.get_pressed()[0] == 1: if pygame.mouse.get_pos()[0] < ballx: dx-=30 elif pygame.mouse.get_pos()[0] > ballx: dx+=30 if pygame.mouse.get_pos()[1] < bally: dy-=30 elif pygame.mouse.get_pos()[1] > bally: dy+=30 if pygame.mouse.get_pressed()[2] == 1: if pygame.mouse.get_pos()[0] < quadratx: qx-=30 elif pygame.mouse.get_pos()[0] > quadratx: qx+=30 if pygame.mouse.get_pos()[1] < quadraty: qy-=30 elif pygame.mouse.get_pos()[1] > quadraty: qy+=30 if quadratx < ballx: dx-=40 elif quadratx > ballx: dx+=40 if quadraty < bally: dy-=40 elif quadraty > bally: dy+=40 if ballx < quadratx: qx-=40 elif ballx > quadratx: qx+=40 if bally < quadraty: qy-=40 elif bally > quadraty: qy+=40 pygame.display.set_caption("Press Space to clean. %.2f frames per second" % clock.get_fps()) # only blit the part of the background where the ball was (cleanrect) #cleanrect = background.subsurface((ballx, bally, ball.get_width(), ball.get_height())) #screen.blit(cleanrect, (ballx, bally)) # comment out this line for a funny effect ! #calculate new center of ball # time based movement. No matter how busy the cpu and how low the framerate, # movement speed will always be constant. ballx += dx * seconds ballx = round(ballx,0) bally += dy * seconds bally = round(bally,0) quadratx += qx * seconds quadratx = round(quadratx,0) quadraty += qy * seconds quadraty = round(quadraty,0) # bounce ball if out of screen if ballx < 0: ballx = 0 dx *= -1 #tock.play() elif ballx + ball.get_width() > screen.get_width(): ballx = screen.get_width() - ball.get_width() dx *= -1 #tock.play() if bally < 0: bally = 0 dy *= -1 #tock.play() elif bally + ball.get_height() > screen.get_height(): bally = screen.get_height() - ball.get_height() dy *= -1 #tock.play() if quadratx < 0: quadratx = 0 qx *= -1 #tock.play() elif quadratx + quadrat.get_width() > screen.get_width(): quadratx = screen.get_width() - quadrat.get_width() qx *= -1 #tock.play() if quadraty < 0: quadraty = 0 qy *= -1 #tock.play() elif quadraty + quadrat.get_height() > screen.get_height(): quadraty = screen.get_height() - quadrat.get_height() qy *= -1 #tock.play() # speed limit if qy > 200: qy=200 if qy < -200: qy=-200 if qx < -200: qx=-200 if qx > 200: qx=200 if dy > 1000: dy=1000 if dy < -1000: dy=-1000 if dx < -1000: dx=-1000 if dx > 1000: dx=1000 if dist < 100: dx=1000 qx=-1000 dy=1000 qy=-1000 #bump.play() # paint the ball ballrot+=deltarot ballgruen+=deltagruen ballblau+=deltablau quadratrot+=quadratdeltarot quadratgruen+=quadratdeltagruen quadratblau+=quadratdeltablau ballrot,deltarot=farbcheck(ballrot,deltarot) #if ballrot > 255: # ballrot=255 # deltarot*=-1 ballgruen,deltagruen=farbcheck(ballgruen,deltagruen) #if ballgruen > 255: # ballgruen=255 # deltagruen*=-1 ballblau,deltablau=farbcheck(ballblau,deltablau) #if ballblau > 255: # ballblau=255 # deltablau*=-1 if ballrot < 0: ballrot=0 deltarot=neudelta() if ballgruen < 0: ballgruen=0 deltagruen=neudelta() if ballblau < 0: ballblau=0 deltablau=neudelta() quadratrot,quadratdeltarot=farbcheck(quadratrot,quadratdeltarot) #if quadratrot > 255: # quadratrot=255 #deltarot*=-1 quadratgruen,quadratdeltagruen=farbcheck(quadratgruen,quadratdeltagruen) #if quadratgruen > 255: # quadratgruen=255 #deltagruen*=-1 quadratblau,quadratdeltablau=farbcheck(quadratblau,quadratdeltablau) #if quadratblau > 255: # quadratblau=255 #deltablau*=-1 if quadratrot < 0: quadratrot=0 deltarot=neudelta() if quadratgruen < 0: quadratgruen=0 deltagruen=neudelta() if quadratblau < 0: quadratblau=0 deltablau=neudelta() # more precise keyboard event handler pressed_keys = pygame.key.get_pressed() if pressed_keys[pygame.K_UP]: qy-=100 if pressed_keys[pygame.K_DOWN]: qy+=100 if pressed_keys[pygame.K_LEFT]: qx-=100 if pressed_keys[pygame.K_RIGHT]: qx+=100 pressed_keys = pygame.key.get_pressed() if pressed_keys[pygame.K_w]: dy-=100 if pressed_keys[pygame.K_s]: dy+=100 if pressed_keys[pygame.K_a]: dx-=100 if pressed_keys[pygame.K_d]: dx+=100 #if pygame.mouse.get_pressed()[0] == 1: # linke maustaste wurde gedrückt #breite +=0.2 #elif pygame.mouse.get_pressed()[2] == 1: #rechte maustaste #breite -=0.2 #breite = min(10,breite) #breite = max(1,breite) #print "quadratrot %i quadratdgruen %i quadreatblau %i " % (quadratrot,quadratgruen,quadratblau) pygame.draw.circle(ball, (ballrot,ballgruen,ballblau), (25,25),25) pygame.draw.rect(quadrat, (quadratrot,quadratgruen,quadratblau), (0,0,50,50)) screen.blit(quadrat, (quadratx, quadraty)) screen.blit(ball, (ballx, bally)) #print "quadratrot %i quadratdgruen %i quadreatblau %i ballrot %i ballgruen %i ballblau %i mixrot %i mixgruen %i mixblau %i " % (quadratrot,quadratgruen,quadratblau, ballrot, ballgruen, ballblau, (ballrot+quadratrot)/2, (ballgruen+quadratgruen)/2, (ballblau+quadratblau)/2) linienfarbe=(ballgruen,quadratgruen) pygame.draw.line(screen, (ballrot,quadratgruen,linienfarbe[random.randint(0,1)]), (ballx+25,bally+25), (quadratx+25,quadraty+25), breite) distx=max(ballx,quadratx)-min(ballx,quadratx) disty=max(bally,quadraty)-min(bally,quadraty) dist = (distx*distx + disty*disty)**0.5 #print"distx %i disty %i dist %i" % (distx,disty,dist) pygame.display.flip() # flip the screen 30 times a second
Ein Ball der rumspringt
ist eigentlich nur eine frühere Version des Hirnschoners
# -*- coding: utf-8 -*- """bouncing ball markus hauser 2009 """ import pygame import random def neudelta(): return random.randint(1, 15) pygame.init() screen=pygame.display.set_mode((640,480)) # try out larger values and see what happens ! background = pygame.Surface(screen.get_size()) background.fill((255,255,255)) #fill the background white (red,green,blue) pygame.draw.circle(background, (50,50,0), (300,250), 200) # draw a circle on the background background = background.convert() ball = pygame.Surface((50,50)) #create a new surface ball.set_colorkey((0,0,0)) #make black the transparent color (red,green,blue) #pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect def loesch(): screen.blit(background, (0, 0)) ballrot=55 ballgruen=200 ballblau=0 deltarot = neudelta() deltagruen = neudelta() deltablau = neudelta() pygame.draw.circle(ball, (ballrot,ballgruen,ballblau), (25,25),25) # paint blue circle ball = ball.convert() ballx = 550 bally = 240 dx = 60 # pixel per second ! dy = 60 # pixel per second ! screen.blit(background, (0,0)) #draw background on screen (overwriting all) screen.blit(ball, (ballx, bally)) #draw the ball shape clock = pygame.time.Clock() mainloop = True FPS = 60 # desired framerate in frames per second. while mainloop: # do all this each frame milliseconds = clock.tick(FPS) # milliseconds passed since last frame seconds = milliseconds / 1000.0 # seconds passed since last frame clock.tick(FPS) # do not go faster than this framerate 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 if pygame.mouse.get_pos()[0] < ballx: dx-=50 elif pygame.mouse.get_pos()[0] > ballx: dx+=50 if pygame.mouse.get_pos()[1] < bally: dy-=50 elif pygame.mouse.get_pos()[1] > bally: dy+=50 pygame.display.set_caption("pygame is running with %.2f frames per second" % clock.get_fps()) # only blit the part of the background where the ball was (cleanrect) cleanrect = background.subsurface((ballx, bally, ball.get_width(), ball.get_height())) #screen.blit(cleanrect, (ballx, bally)) # comment out this line for a funny effect ! #calculate new center of ball # time based movement. No matter how busy the cpu and how low the framerate, # movement speed will always be constant. ballx += dx * seconds ballx = round(ballx,0) bally += dy * seconds bally = round(bally,0) # bounce ball if out of screen if ballx < 0: ballx = 0 dx *= -1 loesch() elif ballx + ball.get_width() > screen.get_width(): ballx = screen.get_width() - ball.get_width() dx *= -1 loesch() if bally < 0: bally = 0 dy *= -1 loesch() elif bally + ball.get_height() > screen.get_height(): bally = screen.get_height() - ball.get_height() dy *= -1 loesch() # paint the ball ballrot+=deltarot ballgruen+=deltagruen ballblau+=deltablau if ballrot > 255: ballrot=255 deltarot*=-1 if ballgruen > 255: ballgruen=255 deltagruen*=-1 if ballblau > 255: ballblau=255 deltablau*=-1 if ballrot < 0: ballrot=0 deltarot=neudelta() if ballgruen < 0: ballgruen=0 deltagruen=neudelta() if ballblau < 0: ballblau=0 deltablau=neudelta() pygame.draw.circle(ball, (ballrot,ballgruen,ballblau), (25,25),25) screen.blit(ball, (ballx, bally)) pygame.display.flip() # flip the screen 30 times a second


