from pygame import * from math import sin, cos, pi init() screen = display.set_mode((1024,700)) class Player: def __init__(self): self.sprite = image.load("tank_square.png") self.upimage = self.sprite self.rect = self.sprite.get_rect() self.vx = 0 self.vy = 0 self.direction = 0 self.angle = 0 def go_up(self): self.vy = -5 self.direction = 180 self.angle = self.direction def go_down(self): self.vy = 5 self.direction = 0 self.angle = self.direction def go_right(self): self.vx = 5 self.direction = 90 self.angle = self.direction def go_left(self): self.vx = -5 self.direction = 270 self.angle = self.direction def stop(self): self.vx = 0 self.vy = 0 def update_and_display(self): self.rect.centerx += self.vx self.rect.centery += self.vy # if we're colliding with a rock: # don't do that if check_collisions(): self.rect.centerx -= self.vx self.rect.centery -= self.vy # Bounce off edges if self.rect.centery < 0 or self.rect.centery > 700: self.vy = -self.vy # Screen wrap, straightforward # if self.rect.centerx < 0: # self.rect.centerx = 1024 # if self.rect.centerx > 1024: # self.rect.centerx = 0 # Screen wrap, shorter way self.rect.centerx = self.rect.centerx % 1024 self.sprite = transform.rotate(self.upimage, self.angle + 180) screen.blit(self.sprite, self.rect) class rock: def __init__(self, px, py): self.sprite = image.load("rock.jpg") self.rect = self.sprite.get_rect() self.rect.centerx = px self.rect.centery = py def display(self): screen.blit(self.sprite, self.rect) p = Player() rocks = [rock(x, y) for x,y in [(300, 300), (100, 500), (600, 100)]] rocks.extend([rock(x,400) for x in range(50, 1000, 41)]) def check_collisions(): collide_rect = p.sprite.get_rect() collide_rect.w = 70 collide_rect.h = 70 collide_rect.centerx = p.rect.centerx collide_rect.centery = p.rect.centery for r in rocks: if r.rect.colliderect(collide_rect): return True return False def shell_results(): for r in rocks: for s in shells: if r.rect.colliderect(s.rect): rocks.remove(r) del r shells.remove(s) del s return shell_velocity = 8 shell_range = 500 class Shell: def __init__(self, direction, x, y): self.direction = direction self.sprite = image.load("shell.png") self.rect = self.sprite.get_rect() self.px = x # Mirror self.rect.centerx with floating point self.py = y # Stands for "Precise X" OR "Position X" self.rect.centerx = x self.rect.centery = y self.travel = 0 def update_and_display(self): dx = sin(self.direction / (180/pi)) * shell_velocity dy = cos(self.direction / (180/pi)) * shell_velocity self.px += dx self.py += dy self.rect.centerx = self.px self.rect.centery = self.py self.travel += shell_velocity screen.blit(self.sprite, self.rect) if self.travel > shell_range: self.delete_self() def delete_self(self): shells.remove(self) del self shells = [] # Main Loop while True: # Event processing (keys) for e in event.get(): if e.type == KEYDOWN: if e.key == K_RIGHT: p.go_right() if e.key == K_LEFT: p.go_left() if e.key == K_UP: p.go_up() if e.key == K_DOWN: p.go_down() if e.key == K_SPACE: shells.append(Shell(p.angle, p.rect.centerx, p.rect.centery)) if e.key == K_d: p.angle += 2 if e.key == K_a: p.angle -= 2 if e.type == KEYUP: if e.key in [K_RIGHT, K_LEFT]: p.vx = 0 if e.key in [K_UP, K_DOWN]: p.vy = 0 if e.type == QUIT: from sys import exit exit() # Update positions and display screen.fill((255, 255, 255)) shell_results() for r in rocks: r.display() p.update_and_display() for s in shells: s.update_and_display() display.flip()